/*
 *  Copyright (C) 2010 MADSA.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author: Jorge Pintado de Santiago
 *  
 */

package com.madsa.tuPeli.option;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.sisof.R;

public class GeneralOptions extends Activity{

	public static SharedPreferences preferenciasAplicacion;
	
	private CheckBox checkSound;
	private CheckBox checkVibration;
	private AutoCompleteTextView cinePreferidoEdit;
	private boolean sound;
	private boolean vibration;
	private String cinePreferido;
	private TextView cinePreferidoText;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.general_options);


        checkSound = (CheckBox) findViewById(R.id.CheckBoxSound);
        checkVibration = (CheckBox) findViewById(R.id.CheckBoxVibration);
        
        String[] listacines = getResources().getStringArray(R.array.cinemaNames);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, listacines);
        cinePreferidoEdit = (AutoCompleteTextView) findViewById(R.id.cinePreferidoEdit);
        cinePreferidoEdit.setAdapter(adapter);
        
        cinePreferidoText = (TextView) findViewById(R.id.cinePreferidoText);
        
        Button okbutton = (Button) findViewById(R.id.okButton);
        okbutton.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				String c = cinePreferidoEdit.getText().toString();
				if (!(c.equals(""))){
					cinePreferidoText.setText(c);
					cinePreferido = c;
				}
			}});
        
        preferenciasAplicacion = PreferenceManager.getDefaultSharedPreferences(this);

        sound = preferenciasAplicacion.getBoolean("haySonido",false);
        vibration = preferenciasAplicacion.getBoolean("hayVibracion",false);
        cinePreferido = preferenciasAplicacion.getString("cinePreferido","");
        
        checkSound.setChecked(sound);
        checkVibration.setChecked(vibration);
        cinePreferidoText.setText(cinePreferido);
    }
	
	public void onStop(){
		super.onStop();
		leerEscribirOpciones();
	}
	
	
	private void actualizarOpciones(SharedPreferences.Editor editorTemp){
        editorTemp.putBoolean("haySonido", sound);
        editorTemp.putBoolean("hayVibracion", vibration);
        editorTemp.putString("cinePreferido", cinePreferido);
        editorTemp.commit(); // Very important
	}
		
	public static boolean getOpcionSonido(){		
		return preferenciasAplicacion.getBoolean("haySonido", false);
	}
	
	public static boolean getOpcionVibracion(){		
		return preferenciasAplicacion.getBoolean("hayVibracion", false);
	}
	
	
	public void leerEscribirOpciones(){
		// Obtenemos las preferencias de la aplicacion desde la ultima ejecucion
        preferenciasAplicacion = PreferenceManager.getDefaultSharedPreferences(this);

        // Update the preferences
        if(checkSound.isChecked()){
        	sound = true;
        }else{
        	sound = false;
        }
               
        if(checkVibration.isChecked()){
        	vibration = true;
        }else{
        	vibration = false;
        }
                 
        // Actualizamos los valores
        SharedPreferences.Editor editor = preferenciasAplicacion.edit();
        actualizarOpciones(editor);
        editor.commit(); // Very important
	}
}